package com.hckj.jianzhijia;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SaveCallback;
import com.hckj.jianzhijia.interfaces.UserInfoChangeInterface;
import com.hckj.jianzhijia.listeners.OnBackClickListener;
import com.hckj.jianzhijia.model.CompanyUser;
import com.hckj.jianzhijia.utils.ExceptionUtils;
import com.hckj.jianzhijia.utils.ToastUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 修改企业姓名的Activity
 */
public class ChangeNameActivity extends BaseActivity implements UserInfoChangeInterface {

    @InjectView(R.id.imgv_go_back)
    ImageView imgvGoBack;
    @InjectView(R.id.edt_company_name)
    EditText edtCompanyName;
    @InjectView(R.id.btn_ok)
    Button btnOk;

    ProgressDialog loadingDialog ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_name);
        ButterKnife.inject(this);
        ((JianzhijiaApplacation) getApplication()).addUserInfoChange(this);
        loadingDialog = new ProgressDialog(this);
        loadingDialog.setCancelable(false);
        loadingDialog.setMessage("正在上传");
        initEvent();
    }

    private void initEvent(){
        imgvGoBack.setOnClickListener(new OnBackClickListener(this));
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //todo 加入其它种类的判断方式
                String newName = edtCompanyName.getText().toString().trim();
                if(newName == null || newName.equals("")){
                    ToastUtils.showToast(getApplicationContext(),"请输入正确的公司名称");
                    return;
                }else{
                    loadingDialog.show();
                    jianzhijiaApplacation.getCompanyUser().setName(newName);
                    jianzhijiaApplacation.getCompanyUser().getAvUser().saveInBackground(new SaveCallback() {
                        @Override
                        public void done(AVException e) {
                            if(e == null){
                                loadingDialog.dismiss();
                                ToastUtils.showToast(getApplicationContext(),"公司名称修改成功");
                                jianzhijiaApplacation.updateCompanyInfo();
                                ChangeNameActivity.this.finish();
                            }else{
                                loadingDialog.dismiss();
                                ToastUtils.showToast(getApplicationContext(), ExceptionUtils.getExceptionInfo(e));
                            }
                        }
                    });
                }
            }
        });
    }

    @Override
    public void updateCompanyInfo() {

    }

    @Override
    public void updateCompanyLogo() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ((JianzhijiaApplacation) getApplication()).removeUserInfoChangeInterface(this);
    }
}
